﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using STMS.STMSApp.UControls;

namespace STMS.STMSApp
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        System.Timers.Timer timer=null;
        int selModuleCode = 1;// 1 store        //2 product        //3 temper
        private void FrmMain_Load(object sender, EventArgs e)
        {
            if (this.Tag!=null)
            {
                InitStatusInfo();//状态栏信息初始化
                ShowStoreManage();//显示仓库管理页面
                btnClosePage.Visible = true;
                RegisterPageBtnClick();

            }
        }

        /// <summary>
        /// 注册分页按钮的单击事件
        /// </summary>
        private void RegisterPageBtnClick()
        {
           //foreach(Control c in panelPageTop.Controls)
           // {
           //     if (c is UPageButton)
           //     {
           //         UPageButton btn =c as UPageButton;
           //         btn.Click += btnPage_Click;
           //     }
           // }
        }

        /// <summary>
        /// 显示仓库管理页面
        /// </summary>
        private void ShowStoreManage()
        {
            //selModuleCode = 1;
            SetModuleBtnBackClolor(1);
            store.FrmStoreList fStoreList = new store.FrmStoreList();
            panelPage.AddPanelForm(fStoreList);
        }

        /// <summary>
        /// 状态栏信息初始化
        /// </summary>
        private void InitStatusInfo()
        {
            string uName=this.Tag.ToString();
            lblLoginUser.Text = uName;

            //时间 动态显示
            lblLoginTime.Text= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            timer = new System.Timers.Timer();           
            timer.Interval = 1000;
            timer.AutoReset = true;     //重复执行
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.Invoke(new Action(() =>           //切换线程，执行委托
        {
            this.lblLoginTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        }));
        }










        private void btnStorePage_Click(object sender, EventArgs e)
        {
            MessageBox.Show("你点击了");
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MsgBoxHelper.MsgBoxConform("退出系统", "确定退出？") == DialogResult.Yes)
            {
                if (timer != null)
                    { 
                    timer.Stop(); 
                    }
                Application.ExitThread();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void btnStoreManage_Click(object sender, EventArgs e)
        {
            ShowStoreManage();//显示仓库管理页面
        }

        private void btnProductManage_Click(object sender, EventArgs e)
        {
            SetModuleBtnBackClolor(2);
            product.FrmProductList fProductList = new product.FrmProductList();
            panelPage.AddPanelForm(fProductList);
        }

        private void btnStoreTemperManage_Click(object sender, EventArgs e)
        {
            SetModuleBtnBackClolor(3);
            storeTemper.FrmStoreRegionTemperatureList fStoreTempertList = new storeTemper.FrmStoreRegionTemperatureList();
            panelPage.AddPanelForm(fStoreTempertList);
        }

        /// <summary>
        /// 设置模块按钮背景色
        /// </summary>
        /// <param name="code"></param>
        private void SetModuleBtnBackClolor(int code)
        {
            switch(code)
            {
                case 1:
                    //设置仓库管理模块按钮的背景色，其他两个按钮背景--默认背景
                    SetMenuBtnsColor(btnStoreManage,btnStorePage);
                    break; 
                case 2:
                    //设置产品管理模块按钮的背景色，其他两个按钮背景--默认背景
                    SetMenuBtnsColor(btnProductManage, btnProductPage);
                    break;
                case 3:
                    //设置仓库温控管理模块按钮的背景色，其他两个按钮背景--默认背景
                    SetMenuBtnsColor(btnStoreTemperManage, btnStoreTemperPage);
                    break;
            }
        }

        /// <summary>
        /// 处理左边按钮背景色和分页按钮的文本
        /// </summary>
        private void SetMenuBtnsColor(UMenuButton btn,UPageButton btnPage)
        {
            //模块按钮背景色，未点击
            Color unSelColor = Color.FromArgb(45, 50, 116);//未选中
            Color SelColor = Color.FromArgb(88, 116, 216);//选中
            
            //分页按钮颜色，
            Color topUnSelColor=Color.FromArgb(45, 50, 116);//点击或显示相关页面时
            Color topSelColor = Color.White;//默认或取消时

            Image topUnSelimage = Properties.Resources.btnbg01;
            Image topSelimage = Properties.Resources.btnbg02;

            btn.BackColor = SelColor;
            btnPage.ForeColor = topSelColor;
            btnPage.BackgroundImage = topSelimage;

            //处理未选中 的模块按钮、未显示的 分页按钮
            UMenuButton[] menuBtns = { btnStoreManage, btnProductManage, btnStoreTemperManage };
            UPageButton[] pageBtns = { btnStorePage, btnSRegionPage, btnProductPage, btnProductInStorePage, btnProductOutStorePage, btnStoreTemperPage };

            foreach (UMenuButton b in menuBtns)
            {
                if (b.Name != btn.Name)
                {
                    b.BackColor = unSelColor;
                    
                }
            }

            foreach (UPageButton b in pageBtns)
            {
                if (btnPage.Name != b.Name)
                {
                    b.ForeColor = topUnSelColor;
                    b.BackgroundImage = topUnSelimage;
                    //https://www.bilibili.com/video/BV1q34y1W7hL/?p=29&spm_id_from=pageDriver&vd_source=aade28408008fe0ffee5016cb40b8051
                }
            }

        }


    }
}
